package com.syh.jn.scm.service.impl.downstream.system;

import cn.redscarf.commons.util.bean.ExtBeanUtils;
import cn.redscarf.commons.util.other.ExtAssert;
import cn.redscarf.commons.util.secret.PasswordUtils;
import cn.redscarf.commons.wrapper.Restrictions;
import cn.redscarf.commons.wrapper.Wrapper;
import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.event.annotation.DomainEventHandle;
import com.syh.jn.scm.domain.downstream.entity.system.DownAccount;
import com.syh.jn.scm.domain.downstream.entity.system.DownAccountOfPhone;
import com.syh.jn.scm.domain.downstream.event.system.DownAccountEvent;
import com.syh.jn.scm.domain.downstream.repository.system.DownAccountOfPhoneReadRepository;
import com.syh.jn.scm.domain.downstream.repository.system.DownAccountOfPhoneRepository;
import com.syh.jn.scm.dto.downstream.system.downAccountOfPhone.DownAccountOfPhoneMto;
import com.syh.jn.scm.dto.downstream.system.downAccountOfPhone.DownAccountOfPhoneSto;
import com.syh.jn.scm.dto.downstream.system.downAccountOfPhone.DownAccountOfPhoneVo;
import com.syh.jn.scm.service.api.downstream.support.enums.DownstreamEnums;
import com.syh.jn.scm.service.api.downstream.system.DownAccountOfPhoneService;
import com.syh.jn.scm.service.api.downstream.system.DownAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by soci.song on 2018/12/13.
 */
@Service
@Transactional
public class DownAccountOfPhoneServiceImpl implements DownAccountOfPhoneService {

    @Autowired
    private DownAccountOfPhoneRepository repository;
    @Resource(name = "downAccountOfPhoneReadRepositoryImpl")
    private DownAccountOfPhoneReadRepository readRepository;
    @Autowired
    private DownAccountService downAccountService;


    @Override
    public Long save(DownAccountOfPhoneSto sto) {
        DownAccountOfPhone account = new DownAccountOfPhone(sto);
        Long id = repository.add(account);
        account.getAccountIds().forEach(accountId ->{
            downAccountService.bindingLoginPhone(accountId,id);
        });
        return id;
    }

    @Override
    public void update(DownAccountOfPhoneMto mto) {
        DownAccountOfPhone entity = readRepository.getById(mto.getId());
        ExtBeanUtils.copyProperties(entity,mto);
        repository.update(entity);
    }

    @Override
    public void delete(Long id) {
        DownAccountOfPhone entity = repository.getById(id);
        entity.destory();
        repository.remove(entity);
    }

    @Override
    public DownAccountOfPhoneVo getById(Long id) {
        return readRepository.getById(id);
    }

    @Override
    public boolean contains(Long id) {
        return readRepository.getById(id) != null;
    }

    @Override
    public List<DownAccountOfPhoneVo> findList(Wrapper wrapper) {
        return this.findListPage(wrapper,null,null);
    }

    @Override
    public List<DownAccountOfPhoneVo> findListPage(Wrapper wrapper, Integer startRow, Integer count) {
        return readRepository.findListPage(wrapper, startRow, count);
    }

    @Override
    public DownAccountOfPhoneVo getOne(Wrapper wrapper) {
        return readRepository.getOne(wrapper);
    }

    @Override
    public int getCount(Wrapper wrapper) {
        return readRepository.getCount(wrapper);
    }

    @Override
    public DownAccountOfPhoneVo verifyAccountPassword(String loginPhone, String password) {
        ExtAssert.notNull(loginPhone, "loginPhone");
        ExtAssert.notNull(password, "密码为空");
        Wrapper wrapper = Wrapper.build();
        wrapper.append(Restrictions.eq("loginPhone", loginPhone));
        wrapper.append(Restrictions.eq("terminalType", DownstreamEnums.AccountType.DOWNSTREAM.toString()));
        DownAccountOfPhone supplierAccount = repository.getOne(wrapper);
        if (supplierAccount == null) {
            return null;
        }
        if (PasswordUtils.verify(password, supplierAccount.getLoginPassword())) {
            DownAccountOfPhoneVo supplierAccountVo = new DownAccountOfPhoneVo();
            ExtBeanUtils.copyProperties(supplierAccount, supplierAccountVo);
            return supplierAccountVo;
        } else {
            return null;
        }
    }

    @Override
    public void binding(Long id, Long accountId) {
        DownAccountOfPhone entity = repository.getById(id);
        entity.binding(accountId);
        repository.update(entity);
    }

    @Override
    public void untying(Long id, Long accountId) {
        DownAccountOfPhone entity = repository.getById(id);
        entity.untying(accountId);
        repository.update(entity);
    }

    @Override
    public void defaultSign(Long id, Long accountId) {
        DownAccountOfPhone entity = repository.getById(id);
        entity.setDefualtSign(accountId);
        repository.update(entity);
    }

    @Override
    public void enable(Long id) {
        DownAccountOfPhone entity = repository.getById(id);
        entity.enable();
        repository.update(entity);
    }

    @Override
    public void disable(Long id) {
        DownAccountOfPhone entity = repository.getById(id);
        entity.disable();
        repository.update(entity);
    }

    @DomainEventHandle({DownAccountEvent.DOWN_ACCOUNT_PHONE_UNTYED,DownAccountEvent.DOWN_ACCOUNT_DISABLED,DownAccountEvent.DOWN_ACCOUNT_DELETED})
    public void accountAlterHandler(DownAccount account) {
        if(account.getLoginPhoneId() == null)
            return;

        DownAccountOfPhone entity = repository.getById(account.getLoginPhoneId());
        if(entity.checkStatus())
            repository.update(entity);
    }
}
